﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Xna.Framework;

namespace UI
{
    public partial class Frm_tombola : Form
    {
        private XNAViewer.XNAViewer tombolone;
        private logic.Tombolone tavola;

        private Button btn_lancia = new Button();
        private Button btn_exit = new Button();
        private Label lbl_msg = new Label();

        private int win=0;

        public Frm_tombola()
        {
            InitializeComponent();

            tavola = new logic.Tombolone(new logic.RandomGenerator()); // Gen tavola
            //
            // Gen TomboloneXNA
            this.Show();
            tombolone = new XNAViewer.XNAViewer();
            tombolone.Crop = true;
            tombolone.Parent = this;
            tombolone.Top = 0;
            tombolone.Left = 0;
            tombolone.Size = new Size(this.Width / 2, this.Height);
            tombolone.CropRectangle = new Microsoft.Xna.Framework.Rectangle(0, 0, tombolone.Size.Width, tombolone.Size.Height);
            tombolone.RunGame(new TomboloneXNAlib.TomboloneXNA(new Microsoft.Xna.Framework.Point(tombolone.Size.Width, tombolone.Size.Height), new TomboloneXNAlib.TomboloneXNA.getTavola(tavola.getTavola)));

            //
            // Gen Message (lbl_msg)
            lbl_msg.Name = "lbl_msg";
            lbl_msg.Parent = this;
            lbl_msg.Top = 100;
            lbl_msg.AutoSize = true;
            lbl_msg.Font = new Font("Comic Sans MS", 20);
            lbl_msg.TextAlign = ContentAlignment.MiddleCenter;
            chgMsg("Benvenuto su TombolaXNA.\nPremere 'Lancia' per iniziare a giocare.");

            //
            // Gen Lancia (btn_lancia)
            btn_lancia.Name = "btn_lancia";
            btn_lancia.Parent = this;
            btn_lancia.Top = lbl_msg.Top + lbl_msg.Height + 50;
            btn_lancia.AutoSize = true;
            btn_lancia.Font = new Font("Comic Sans MS", 15);
            btn_lancia.Text = "Lancia";
            btn_lancia.Show();
            btn_lancia.Left = tombolone.Size.Width + (tombolone.Size.Width - btn_lancia.Width) / 2;
            btn_lancia.BackColor = System.Drawing.Color.Red;
            btn_lancia.Click += new EventHandler(btn_lancia_click);

            //
            // Gen Exit (btn_exit)
            btn_exit.Name = "btn_exti";
            btn_exit.Parent = this;
            btn_exit.Text = "Esci";
            btn_exit.Font = new Font("Comic Sans MS", 10);
            btn_exit.AutoSize = true;
            btn_exit.Show();
            btn_exit.Left = this.Width - btn_exit.Width - 20;
            btn_exit.Top = this.Height - btn_exit.Height - 20;
            btn_exit.Click += new EventHandler(btn_exit_click);

        }

        //~Frm_tombola()
        //{
        //    tombolone.StopGame();
        //    tombolone.Dispose();
        //}

        private void chgMsg(string messaggio)
        {
            lbl_msg.Text = messaggio;
            lbl_msg.Show();
            lbl_msg.Left = this.tombolone.Size.Width + (this.tombolone.Size.Width - lbl_msg.Width) / 2;
        }   // Cambia il messaggio della Label

        private void btn_lancia_click(object sender, EventArgs e)
        {
            if (win == 6)
            {
                chgMsg("Gioco Finito!");
                return;
            }

            tavola.estrai();
            chgMsg((tavola.getNumber()+1).ToString());
            if (tavola.getWin() > win)
            {
                win = tavola.getWin();
                switch (win) 
                {
                    case (2):
                        chgMsg((tavola.getNumber() + 1).ToString() + "\nAmbo!!");
                        break;
                    case (3):
                        chgMsg((tavola.getNumber() + 1).ToString() + "\nTerno!!");
                        break;
                    case (4):
                        chgMsg((tavola.getNumber() + 1).ToString() + "\nQuaterna!!");
                        break;
                    case (5):
                        chgMsg((tavola.getNumber() + 1).ToString() + "\nCinquina!!");
                        break;
                    case (6):
                        chgMsg((tavola.getNumber() + 1).ToString() + "\nTombola!!");
                        break;
                }
            }
        }

        private void btn_exit_click(object sender, EventArgs e)
        {
            Form response = new Frm_ExitDialog();
            response.ShowDialog();

            if (response.DialogResult == DialogResult.Cancel)
                ;
            else if (response.DialogResult == DialogResult.Yes)
            {
                tombolone.StopGame();
                this.win = 0;
                this.tavola = new logic.Tombolone(new logic.RandomGenerator());
                tombolone.RunGame(new TomboloneXNAlib.TomboloneXNA(new Microsoft.Xna.Framework.Point(tombolone.Size.Width, tombolone.Size.Height), new TomboloneXNAlib.TomboloneXNA.getTavola(this.tavola.getTavola)));
                chgMsg("Premere 'Lancia' per iniziare a giocare.");
            }
            else if (response.DialogResult == DialogResult.No)
                Application.Exit();

            response.Dispose();

        }   // Esci
    }
}
